package cn.tedu.order.service;

import cn.tedu.order.entity.Order;
import cn.tedu.order.feign.AccountClient;
import cn.tedu.order.feign.EasyIdClient;
import cn.tedu.order.feign.StorageClient;
import cn.tedu.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private EasyIdClient easyIdClient;
    @Autowired
    private StorageClient storageClient;
    @Autowired
    private AccountClient accountClient;
    @Override
    public void create(Order order) {
        //TODO:远程调用ID发号器,获取订单id
        Long id = Long.valueOf(easyIdClient.nextId("order_business"));
//        Long orderId = Long.valueOf(new Random().nextInt(Integer.MAX_VALUE));
//        long id = Math.abs(new Random().nextLong());
        order.setId(id);
        orderMapper.create(order);
        //TODO:远程调用仓库,减少库存
        storageClient.decrease(order.getProductId(),order.getCount());

        //TODO:远程调用账户,扣减账户
        accountClient.decrease(order.getUserId(),order.getMoney());
    }
}
